package com.mail.controller;

import com.mail.common.DataResult;
import com.mail.dto.EmailTemplatesDto;
import com.mail.service.EmailTemplatesService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/email/templates")
public class EmailTemplatesController {
    @Resource
    private EmailTemplatesService emailTemplatesService;

    /**
     * 添加邮件模版
     */
    @PostMapping("/addEmailTemplates")
    DataResult addEmailTemplates (EmailTemplatesDto emailTemplatesDto){
        return emailTemplatesService.addEmailTemplates(emailTemplatesDto);
    }


    /**
     * 修改邮件模版
     */
    @PutMapping("/updateEmailTemplates")
    DataResult updateEmailTemplates(@RequestParam Map<String,Object> map){
        String id = (String) map.get("id");
        if (id.length() == 0) {
            return DataResult.error("请输入id！");
        }
        return emailTemplatesService.updateEmailTemplates(map);
    }

    /**
     * 删除邮件模版
     */
    @DeleteMapping("/deleteEmailTemplates/{id}")
    DataResult deleteEmailTemplates(@PathVariable long id){
        return emailTemplatesService.deleteEmailTemplates(id);
    }

    /**
     分页查询邮件模版
     */
    @GetMapping("/queryEmailTemplatesPage")
    DataResult queryEmailTemplatesPage(@RequestParam Map<String,Object> map){
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        if (page.length() == 0 || limit.length() == 0) {
            return DataResult.error("请传入参数！");
        }
        if (Long.parseLong(page) < 0 || Long.parseLong(limit) < 0) {
            return DataResult.error("请传入正确的参数！");
        }
        return emailTemplatesService.queryEmailTemplatesPage(map);
    }
    /**
     分页查询某个用户下的邮件模版
     */
    @GetMapping("/queryEmailTemplatesPersonal")
    DataResult queryEmailTemplatesPersonal(@RequestParam Map<String,Object> map){
        String page = (String) map.get("page");
        String limit = (String) map.get("limit");
        if (page.length() == 0 || limit.length() == 0) {
            return DataResult.error("请传入参数！");
        }
        if (Long.parseLong(page) < 0 || Long.parseLong(limit) < 0) {
            return DataResult.error("请传入正确的参数！");
        }
        return emailTemplatesService.queryEmailTemplatesPersonal(map);
    }
}
